package com.example.springsearchengines.controller;

import com.example.springsearchengines.service.Result;
import com.example.springsearchengines.service.DocSearcherService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Component
public class DocSearchController {

    private ObjectMapper objectMapper = new ObjectMapper();

    private DocSearcherService docSearcherService = new DocSearcherService();

    @RequestMapping(value = "/searcher",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String searcher(@RequestParam("query") String query) throws JsonProcessingException {

        //参数是查询词，返回结果是json字符串
        List<Result> results = docSearcherService.search(query);
        return objectMapper.writeValueAsString(results);
    }

}
